package com.sgb.kjwl.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.DialogSetGoodsweightBinding;
import com.swgk.core.util.MToast;


public class SetGoodsWeightDialog extends Dialog implements View.OnClickListener {
    private static final float MIN_VALUE = 0f;
    private static final float MAX_VALUE = 999.9f;


    private DialogSetGoodsweightBinding binding;
    SetGoodWeightDialogListener listener;


    public interface SetGoodWeightDialogListener {
        void onSureClick(float min, float max);
    }


    public SetGoodsWeightDialog(Context context, SetGoodWeightDialogListener listener) {
        super(context, R.style.RoundCornerDialog);
        this.listener = listener;

        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_set_goodsweight, null, false);
        setContentView(binding.getRoot());
        init();
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
        }
    }

    private void init() {
        initView();
        initEvent();
    }

    private void initView() {
        binding.weightMin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateValueToValidValue(binding.weightMin, charSequence);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.weightMax.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateValueToValidValue(binding.weightMax, charSequence);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


    }

    private void updateValueToValidValue(EditText view, CharSequence charSequence) {
        if (charSequence == null || charSequence.length() == 0) {
            return;
        }
        float value = Float.valueOf(charSequence.toString());
        if (value < MIN_VALUE) {
            view.setText(0);
            view.setSelection(view.getText().length());
        }
        if (value > MAX_VALUE) {
            view.setText(String.valueOf(MAX_VALUE));
            view.setSelection(view.getText().length());
        }
    }


    private float getMinValue() {
        if (TextUtils.isEmpty(binding.weightMin.getText().toString())) {
            return MIN_VALUE;
        }
        return Float.valueOf(binding.weightMin.getText().toString());
    }

    private float getMaxValue() {
        if (TextUtils.isEmpty(binding.weightMax.getText().toString())) {
            return MAX_VALUE;
        }
        return Float.valueOf(binding.weightMax.getText().toString());
    }


    private void initEvent() {
        binding.tvReset.setOnClickListener(this);
        binding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_reset:
                binding.weightMax.setText("");
                binding.weightMin.setText("");
                break;
            case R.id.tv_confirm:
                if (isUnSetMinAndMax()) {
                    dismiss();
                    return;
                }
                if (minAndMaxIsValid()) {
                    this.listener.onSureClick(getMinValue(), getMaxValue());
                    dismiss();
                } else {
                    MToast.showLongToast(getContext(), "最小值不能大于最大值");
                }
                break;
        }
    }

    private boolean minAndMaxIsValid() {
        return getMinValue() <= getMaxValue();
    }

    private boolean isUnSetMinAndMax() {
        return TextUtils.isEmpty(binding.weightMax.getText().toString()) &&
                TextUtils.isEmpty(binding.weightMin.getText().toString());
    }


}
